/*****************************************************************************\

File:   otkCreateSection3D.java

Date          Build     Author        Version   Comments
28-Aug-12    P-20-13    asonar         $$1       Created
19-Oct-12    P-20-15    asonar         $$2       Updated for qcr
20-mar-15 P-30-05 gshmelev $$3  called GetCurrentSession

\****************************************************************************/

package com.ptc.otkjavaexamples;

import com.ptc.cipjava.*;
import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcCommand.*;
import com.ptc.pfc.pfcGlobal.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcSolid.*;
import java.io.*;

import com.ptc.pfc.pfcFamily.*;
import com.ptc.pfc.pfcWindow.*;
import com.ptc.pfc.pfcSection.*;
import com.ptc.wfc.wfcSection.*; 
import com.ptc.wfc.wfcSession.*;
import com.ptc.pfc.pfcBase.*;

/*====================================================================*\
  FUNCTION : otkCreateSection3D
  PURPOSE  : This example will demonstrate manipulating a 3D Section.
  It will first get section present in sketcher session.
  It will then add/delete entities to/from it.
  It will finally set it active to reflect the changes.
  \*====================================================================*/

class otkCreateSection3D extends DefaultUICommandActionListener
{
    
    public otkCreateSection3D()
    {
    }
    
    public void OnCommand()
    {
	addCreateSection3D();
    }
    
    public void addCreateSection3D()
    {
		
      	try
	    {
		FileWriter file = new FileWriter ("3D_Section.txt");
		BufferedWriter fp_out_3D_Section = new BufferedWriter(file);	    
    
		Session session = pfcSession.GetCurrentSession ();      
		WSession wSession = (WSession)session;
		
		/*
		  Get an active section from current sketcher session
		  
		  NOTE 1: We must be in sketcher mode to get an active section
		  otherwise method will throw an pfcXToolkitBadContext exception
		  
		  NOTE 2: GetActiveSection() always return a copy of the section object
		  
		  NOTE 3: If you modify this section and wish to apply changes
		  you must set it back using method SetActiveSection()	   
		*/
		
		Section section3d = wSession.GetActiveSection();
		
		/*
		  Modify section by adding 2D entities to the section
		  
		  NOTE : Before manipulating any section we must turn ON intent manager
		  to ensure section resolution done properly
		  
		*/      
		section3d.SetIntentManagerMode(true);
		
		section3d.SetEpsilon(0.50);
		
		AddEntitiestoSection(section3d);
		
		section3d.SetActive();
		
		section3d = wSession.GetActiveSection();
		
		/*
		  List entities in the section.
		  Filter list to get ARC entity 
		  
		  NOTE : To delete or get any entity we must know its identifier
		  GetEntityIds() returns identifier of all entities  
		  present in the section
		  
		*/
		
		fp_out_3D_Section.write ("\nName of the Section " + section3d.GetName() );
		fp_out_3D_Section.newLine();
		
		SectionEntities entities = section3d.ListSectionEntities();
		fp_out_3D_Section.write("\nNumber of entities in the section " + entities.getarraysize() );
		fp_out_3D_Section.newLine();
		
		for(int ii=0; ii<entities.getarraysize(); ii++)
		    {
			
			fp_out_3D_Section.write ("\n\t Id of entity " + (ii+1) + " is " + 
						 section3d.GetEntityIds().get(ii) );
			
			fp_out_3D_Section.newLine();	
			
		    }
		
		for(int ii=0; ii<entities.getarraysize(); ii++)
		    {
			if(entities.get(ii).GetSectionEntityType() == 
			   Section2dEntType.SEC_ENTITY_2D_ARC)
			    {
				section3d.DeleteEntity(section3d.GetEntityIds().get(ii));
				break;
			    }
		    }
		
		/*
		  Set modified section as an active section
		  
		  NOTE 1: We must take care of validity of section
		  
		  NOTE 2: Copy of the section is set as active
		  
		  NOTE 3: Section will also be added to UNDO/REDO stack
		*/
		section3d.SetActive();
		
		section3d.SetIntentManagerMode(false);
		
		fp_out_3D_Section.close();
		
		
	    }
	catch (Exception e)
	    {
		System.out.println("Exception: " + e);
	    }
    }
    
    public static void AddEntitiestoSection(Section section)
    {
	
	try
	    {
		Point2D point = wfcSession.CreatePoint2D(100.00,100.00);
		
		SectionEntityPoint  pointEnt = wfcSection.SectionEntityPoint_Create(point);
		SectionEntity entity = (SectionEntity)pointEnt;
		section.AddEntity(entity);
		
		Outline2D line = wfcSession.CreateOutline2D(10.00,10.00,150.00,150.00);
		
		SectionEntityLine lineEnt = wfcSection.SectionEntityLine_Create(line);
		entity = (SectionEntity)lineEnt;
		section.AddEntity(entity);
		
		SectionEntityCircle circleEnt = wfcSection.SectionEntityCircle_Create
		    (point,200.50);
		entity = (SectionEntity)circleEnt;
		section.AddEntity(entity);
		
		SectionEntityEllipse ellipseEnt = wfcSection.SectionEntityEllipse_Create
		    (point,150.00,200.00);
		entity = (SectionEntity)ellipseEnt;
		section.AddEntity(entity);
		
		SectionEntityArc arcEnt = wfcSection.SectionEntityArc_Create
		    (point,1.57,3.14,50.00);
		entity = (SectionEntity)arcEnt;
		section.AddEntity(entity);
		
		line = wfcSession.CreateOutline2D(100.00,100.00,150.00,150.00);
		
		SectionEntityCenterLine cLine = wfcSection.SectionEntityCenterLine_Create
		    (line);
		entity = (SectionEntity)cLine;
		section.AddEntity(entity);
		
	    }
	
	catch (Exception e)
	    {
		System.out.println("Exception: " + e);
	    }
	
    }
    
}

